

        var browser = {};



        var ua = navigator.userAgent.toLowerCase();



        browser.msie = (/msie ([\d.]+)/).test(ua);

        browser.firefox = (/firefox\/([\d.]+)/).test(ua);

        browser.chrome = (/chrome\/([\d.]+)/).test(ua);



        function SetNodeStyle(doc, node, name, value)

        {

            if (node.innerHTML == undefined)

            {

                return node;

            }

            else

            {

                node.style[name] = value;



                for (var i = 0; i < node.childNodes.length; i++)

                {

                    var cn = node.childNodes[i];

                    if (node.innerHTML != undefined)

                    {

                        SetNodeStyle(doc, cn, name, value);

                    }

                }



                return node;

            }

        }



        function SetStyle(doc, html, name, value)

        {

            var dom = doc.createElement("DIV");

            dom.innerHTML = html;



            for (var i = 0; i < dom.childNodes.length; i++)

            {

                var node = dom.childNodes[i];



                if (node.innerHTML == undefined)

                {

                    var span = doc.createElement("SPAN");

                    span.style[name] = value;

                    if (node.nodeValue != undefined) span.innerHTML = node.nodeValue.replace(/\</ig, function() { return "&lt;"; });

                    else if (node.textContent != undefined) span.innetHTML = node.textContent.replace(/\</ig, function() { return "&lt;"; });

                    dom.replaceChild(span, node);

                }

                else

                {

                    SetNodeStyle(doc, node, name, value);

                }

            }



            return dom.innerHTML;

        }

//获取多个节点的HTML

        function GetInnerHTML(nodes)

        {

            var builder = [];

            for (var i = 0; i < nodes.length; i++)

            {

                if (nodes[i].innerHTML != undefined)

                {

                    builder.push(nodes[i].innerHTML);

                }

                else

                {

                    if (nodes[i].textContent) builder.push(nodes[i].textContent.replace(/\</ig, function() { return "&lt;"; }));

                    else if (nodes[i].nodeValue) builder.push(nodes[i].nodeValue.replace(/\</ig, function() { return "&lt;"; }));

                }

            }

            return builder.join("");

        }



        function SelectionRange(doc, range)

        { //获取选中的内容的HTML

            this.GetSelectedHtml = function()

            {

                if (range == null) return "";

                

                if (browser.msie)

                {

                    if (range.htmlText != undefined) return range.htmlText;

                    else return "";

                }

                else if (browser.firefox || browser.chrome)

                {

                    return GetInnerHTML(range.cloneContents().childNodes);

                }

                else

                {

                    return "";

                }

            }

 //用html替换选中的内容的HTML

            this.Replace = function(html)

            {

                if (range != null)

                {

                    if (browser.msie)

                    {

                        if (range.pasteHTML != undefined)

                        { //当前选中html可能以为某种原因（例如点击了另一个DIV）而丢失，重新选中

                            range.select();

                            range.pasteHTML(html);

                            return true;

                        }

                    }

                    else if (browser.firefox || browser.chrome)

                    {

                        if (range.deleteContents != undefined && range.insertNode != undefined)

                        { //将文本html转换成DOM对象

                            var temp = doc.createElement("DIV");

                            temp.innerHTML = html;



                            var elems = [];

                            for (var i = 0; i < temp.childNodes.length; i++)

                            {

                                elems.push(temp.childNodes[i]);

                            }

 //删除选中的节点

                            range.deleteContents();

 //将html对应的节点(即temp的所有子节点)逐个插入到range中，并从temp中删除

                            for (var i in elems)

                            {

                                temp.removeChild(elems[i]);

                                range.insertNode(elems[i]);

                            }

                            return true;

                        }

                    }

                }

                return false;

            }

        }

//与此同时，还实现了一个函数GetSelectionRange用于获取当前选中文本对应的SelectionRange对象，

        function GetSelectionRange(win)

        {

            var range = null;



            if (browser.msie)

            {

                range = win.document.selection.createRange();

                if (range.parentElement().document != win.document)

                {

                    range = null;

                }

            }

            else if (browser.firefox || browser.chrome)

            {

                var sel = win.getSelection();

                if (sel.rangeCount > 0) range = sel.getRangeAt(0); else range = null;

            }



            return new SelectionRange(win.document, range);

        }



        function RichEditor(container, width, height)

        {

			

            container.innerHTML =
            '<iframe frameborder="0" style="padding: 0px;"></iframe>';



//            var toolbar = container.childNodes[0];

            var editor = container.childNodes[0];



            var editorDoc = editor.contentWindow.document;

            var editorWindow = editor.contentWindow;



            toolbar.style.width = width + 'px';

            toolbar.style.height = '24px';



            editor.style.width = width + 'px';

            editor.style.height = (height - 24) + 'px';



            if (browser.firefox)

            {

                editor.onload = function()

                {

                    editorDoc.designMode = "on";

                }

            }

            else

            {

                editorDoc.designMode = "on";

            }



            editorDoc.open();

            editorDoc.write("<html><head></head><body style='margin:0px; padding: 0px;'></body></html>");

            editorDoc.close();

            toolbar.childNodes[0].onclick = function()

            {

                var src = prompt("请输入图片的URL:", "");

                if (src != null)

                {

                    var range = GetSelectionRange(editorWindow);

                    range.Replace("<img src='" + src + "'></img>");

                }

            }



            toolbar.childNodes[1].onclick = function()

            {

                editorDoc.execCommand("Bold", false, null);

            }



            toolbar.childNodes[2].onclick = function()

            {

               // editorDoc.execCommand("Underline", false, null);editorDoc.execCommand("Bold", false, null);

            }



            toolbar.childNodes[3].onclick = function()

            {

               // editorDoc.execCommand("Italic", false, null);

            }





            toolbar.childNodes[5].onchange = function()

            {

                var range = GetSelectionRange(editorWindow);

				

                var html = SetStyle(editorDoc, range.GetSelectedHtml(), "fontSize", toolbar.childNodes[5].value);

                range.Replace(html);

            }



            toolbar.childNodes[7].onchange = function()

            {

                var range = GetSelectionRange(editorWindow);

                var html = SetStyle(editorDoc, range.GetSelectedHtml(), "fontFamily", toolbar.childNodes[7].value);

                range.Replace(html);

            }

			toolbar.childNodes[8].onclick = function(){

				  var range = GetSelectionRange(editorWindow);

				

				 var html = SetStyle(editorDoc, range.GetSelectedHtml(), "fontSize", 22);

                range.Replace(html);

			}

			

        }




	
